<!DOCTYPE html>
<html lang="ch">
<head>
    <meta charset="UTF-8">
    <title>修改店铺信息</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/addStore.css">
    <link rel="stylesheet" href="css/registerCommon.css">
</head>
<body>
<div class="container">
    <!-- 登录对话框 -->
    <div class="dialog">
        <h2>修改店铺信息</h2>

        <div class="row">
            <span>店铺名称</span>
            <input type="text" id="storeName" placeholder="店铺名称">
        </div>

        <div class="row">
            <span>店铺简介</span>
            <input type="text" id="storeIntroduction" placeholder="请输入简介">
        </div>
        <div class="row">
            <span>联系方式</span>
            <input type="number" id="contactInfo" placeholder="请输入联系方式">
        </div>
        <div class="row">
            <button class="submit" onclick="updateStore()">提交</button>
        </div>

    </div>

</div>

<script src="js/jquery.min.js"></script>
<script src="js/store.js"></script>
<script>
    let userId = localStorage.getItem("userId");
    if (userId === null || userId === "") {
        alert("请先登录");
        location.href = "/login.html";
    }
    let queryString = location.search;
    // 创建 URLSearchParams 对象
    let urlParams = new URLSearchParams(queryString);
    let storeId = urlParams.get("storeId");

    selectStoreByUserIdAndStoreId(userId, storeId, fillPar);
    function fillPar(store) {
        $("#storeName").val(store.storeName);
        $("#storeIntroduction").val(store.storeIntroduction);
        $("#contactInfo").val(store.contactInfo);
    }
    function updateStore() {
        //校验参数
        let pass = true;
        $(' input').each(function () {
            let inputValue = $(this).val();
            if (inputValue === null || inputValue === "") {
                const labelText = $(this).prev('span').text();
                pass = false;
                alert("请输入" + labelText);
                return false;
            }
        });
        if (!pass) {
            return;
        }
        $.ajax({
            type: 'post',
            url: '/store/updateStore',
            data: {
                userId: userId,
                storeId: storeId,
                storeName: $("#storeName").val(),
                storeIntroduction: $("#storeIntroduction").val(),
                contactInfo: $("#contactInfo").val()
            }
            , success: function (result) {
                if (result.code === "SUCCESS") {
                    alert("修改成功");
                    history.back();
                } else {
                    alert(result.errMsg);
                    $("#imgCode").click();
                }
            }
            , error: function (error) {
                if (error != null && error.status === 401) {
                    alert("请先登录");
                    location.href = "/login.html";
                } else {
                    alert("内部错误");
                }
            }
        });
    }
</script>
</body>
</html>